package forum.temp.client;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import forum.business.entities.UserBO;
import forum.business.facades.UserFacade;
import forum.util.exceptions.AdException;

public class MyClient {

	public MyClient() {
	}

	public void createUser(String login, String status,  String password,
			String email, String city, String country, Date birthDate,
			Boolean isFemale, Integer postsCounter, Date joinDate, String name,
			String surname) {

		UserBO userBO = new UserBO();

		userBO.setLogin(login);
		userBO.setStatus(status);
		
		userBO.setPassword(password);
		userBO.setEmail(email);
		userBO.setCity(city);
		userBO.setCountry(country);
		userBO.setBirthDate(birthDate);
		userBO.setIsFemale(isFemale);
		userBO.setPostsCounter(postsCounter);
		userBO.setJoinDate(joinDate);
		userBO.setJoinDate(joinDate);
		userBO.setName(name);
		userBO.setSurname(surname);
		
		UserFacade userFacade = new UserFacade();

		try {
			userFacade.createUser(userBO);
		} catch (AdException e) {
			System.err
					.println("Klient sygnalizuje problem z utworzeniem uzytkownika: "
							+ e.getMessage());
		}
	}
	
	public UserBO getUser(String login){
		UserFacade userFacade = new UserFacade();
		UserBO userBO = null;
		try {
			userBO = userFacade.getUser(login);
			System.out.println("Imie " + userBO.getName() + " Nazwisko " + userBO.getSurname());
		} catch (AdException e) {
			System.err.println("Nie mozna pobrac uzytkownika "+ e.getMessage() );
		}
		return userBO;
	}
	public List listUsers(){
		UserFacade userFacade = new UserFacade();
		List users = null;
		try {
			users = userFacade.listUsers();
		} catch (AdException e) {
			System.err
					.println("Nie mozna wyswietlic uzytkownikow uzytkow: "
							+ e.getMessage());
		}
		return users;
		
	}
	
	public void removeUser(String login){
		UserFacade userFacade = new UserFacade();
		try {
			userFacade.removeUser(login);
		} catch (AdException e) {
			System.err.println("Nie mozna usunac uzytkownika "+e.getMessage());
		}
	}

	public void updateUser(UserBO userBO){
		UserFacade userFacade = new UserFacade();
		try {
			userFacade.updateUser(userBO);
		} catch (NullPointerException e) {
			System.err.println("Aktualizacja uyztkownika przerwana poniewz uzytkownik o podanym loginie nie istnieje "+e.getMessage());
		} catch (AdException e) {
			System.err.println("Nie mozna uaktulanic uzytkownika "+e.getMessage());
		}
	}
	
	
	public static void main(String[] args) {
		MyClient myClient = new MyClient();
		myClient.createUser("matrix", "USER", "pas2", "matrix@op.pl", "Warszawa", "Poland", new Date(), new Boolean(false), new Integer(0), new Date(), "Marcin", "J�drzejewski");
		myClient.createUser("cck", "USER", "pas1", "cck@op.pl", "Wroc�aw", "Poland", new Date(), new Boolean(true), new Integer(0), new Date(), "Pawel", "Cyckiewicz");
		myClient.createUser("kojak", "USER", "pas3", "kojak@op.pl", "Wroc�aw", "Poland", new Date(), new Boolean(false), new Integer(0), new Date(), "Karol", "Babiak");
		
		UserBO user = myClient.getUser("matrix");
		user.setName("Mateusz");
		myClient.updateUser(user);
		
		
		List users = myClient.listUsers();
		for (Iterator iter = users.iterator(); iter.hasNext();) {
			UserBO userBO = (UserBO) iter.next();
			System.out.println("Uzytkownik!!!!!!!: "+ userBO.getLogin() + " "+userBO.getName() + " " + userBO.getSurname() +" "+ userBO.getStatus());
		}
		myClient.getUser("aaax");
	}
}